package sdu.attendancechecker.models;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import sdu.attendancechecker.dto.Schedule;
import sdu.attendancechecker.dto.Teacher;
import sdu.attendancechecker.services.DbService;

public class ScheduleModel {
	public static ArrayList<Schedule> getSchedule(String subject, int teacher) {
		Connection conn = DbService.getConnection();
		if (conn == null)
			return null;
		String sql = "SELECT * FROM schedule WHERE subject = ? AND teacher = ?";
		try {
			ArrayList<Schedule> list = new ArrayList<Schedule>();
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setString(1, subject);
			pstmnt.setInt(2, teacher);
			ResultSet result = pstmnt.executeQuery();
			while (result.next()) {
				Schedule schedule = new Schedule();
				schedule.setSubject(subject);
				schedule.setTeacher(teacher);
				schedule.setClassroom(result.getString("classroom"));
				schedule.setWeek(result.getInt("week"));
				schedule.setStart(result.getTime("start"));
				schedule.setEnd(result.getTime("end"));
				list.add(schedule);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
